/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.repository.dac.EntityDacGenContext;
import com.inspur.edp.cef.repository.dac.ChildEntityDac;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class GetChildDacMethodGenerator extends ClassMethodGenerator {

  private static final String ChildLiatVarName = "childDacList";
  private EntityDacGenContext dacContext;

  public GetChildDacMethodGenerator(EntityDacGenContext dacContext) {
    this.dacContext = dacContext;
  }

  @Override
  protected String getMethodName() {
    return "getChildDacList";
  }

  @Override
  protected TypeInfo getReturnType() {
    TypeInfo typeInfo = new TypeInfo(java.util.List.class.getTypeName());
    typeInfo.setIsParameterizedType(true);
    typeInfo.getArguments().add(new TypeInfo(ChildEntityDac.class));
    return typeInfo;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    buildInitListStatement(block);
    buildAddItemsStatements(block);
    buildReturnStatement(block);
    return block;
  }

  private void buildInitListStatement(Block block) {
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("list"));

    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();

    ParameterizedType parameterizedType1 = ast.newParameterizedType(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ArrayList.class)
            .getType(ast, dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));
    parameterizedType1.typeArguments().add(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ChildEntityDac.class)
            .getType(ast, dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));
    classInstanceCreation.setType(parameterizedType1);
    variableDeclarationFragment.setInitializer(classInstanceCreation);

    VariableDeclarationStatement variableDeclarationStatement = ast
        .newVariableDeclarationStatement(variableDeclarationFragment);
    ParameterizedType parameterizedType = ast.newParameterizedType(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(List.class)
            .getType(ast, dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));
    parameterizedType.typeArguments().add(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ChildEntityDac.class)
            .getType(ast, dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));

    variableDeclarationStatement.setType(parameterizedType);
    block.statements().add(variableDeclarationStatement);
  }

  private void buildAddItemsStatements(Block block) {
    if (dacContext.getChildDacTypes() == null || dacContext.getChildDacTypes().size() < 1) {
      return;
    }
    for (Map.Entry<String, String> childDacType : dacContext.getChildDacTypes().entrySet()) {
      buildAddItemStatement(block, childDacType);
    }
  }

  private void buildAddItemStatement(Block block, Map.Entry<String, String> childDacType) {
    String varName = childDacType.getKey() + "dac";
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName("add"));
    methodInvocation.setExpression(ast.newSimpleName("list"));

    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(varName)));
    classInstanceCreation.arguments().add(ast.newThisExpression());

    methodInvocation.arguments().add(classInstanceCreation);
    block.statements().add(ast.newExpressionStatement(methodInvocation));
  }

  private void buildReturnStatement(Block block) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("list"));
    block.statements().add(returnStatement);
  }
}
